/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/

/**
	Module with base layout for containers
	@module jgrouse.widgets.Layout
	@requires jgrouse.util.ObjectMap
*/

jgrouse.module({
	name : 'jgrouse.widgets.Layout',
	requires : ['jgrouse.widgets.Widget'],
	imports : ['jgrouse.util.ObjectMap'],
	body : function(embed)
	{
		eval(embed);
		
		/**
			Class that is used to layout widgets in container.
			@class jgrouse.widgets.Layout
			@extends jgrouse.BaseObject
		*/
		jgrouse.define('jgrouse.widgets.Layout', 'jgrouse.BaseObject', function(embed)
		{
			eval(embed);
			
			return {
				initialize : function()
				{
					_sup(this, arguments);
					/**
						Map with all widgets in layout.
						Keys are constraints, values are widgets.
						Note - review that in order to allow multiple widgets to be bound to same constraints
						@variable {jgrouse.util.Map} _widgetsMap
					*/
					this._widgetsMap = new ObjectMap();				
					
				},
				
				/**
					Add widget to layout
					@function addWidget
					@param {jgrouse.widget.Widget} widget - widget to be added
					@param {Object} constraint - indicator where the widget should be added. Type and interpretation
					of that parameter depends on actual implementation of layout
				*/
				addWidget : function(widget, constraint)
				{
					this._widgetsMap.put(constraint, widget);
				},
				
				/**
					Perform pre-processing of data in layout and create necessary widgets
					@function doParse
				*/
				doParse : function()
				{
					jgrouse.notImplemented(this.RTTI + ".doParse");
				},
				
				/**
					Render the contend of layout into given dom element
					@function render
					@param {DOMElement} parentDom
				*/
				render : function(parentDom)
				{
				},
				
				/**
					Remove the contents of layout from given dom element
					@function remove
					@param {DOMElement} parentDom
				*/
				remove : function(parentDom)
				{
					this.forEach(function(widget)
					{
						widget.setShowing(false);
					});
				},
				
				/**
					Remove widget from layout and optionally dispose it
					@function {Object} removeWidget
					@param {jgrouse.widget.Widget} widget - widget to be removed
					@param {boolean} doDispose - indicator if widget should be disposed
					@returns constraint to which the widget was attached
				*/
				removeWidget : function(widget, doDispose)
				{
					var constraint = null;
					this.forEach(function(w, d)
					{
						if (w == widget)
						{
							constraint = d;
							throw jgrouse.$break;
						}
					});
					if (constraint !== null)
					{
						this._widgetsMap.remove(constraint);
					}
					widget.remove(doDispose);
					return constraint;
				},
				
				/**
					Dispose layout and all associated resources and widgets
					@function dispose
				*/
				dispose : function()
				{
					this.forEach(function(widget, dom)
					{
						widget.remove(true);
					});
					this._widgetsMap.clear();
				
				},

				/**
					Return map containing all widgets and DOM elements that should become members of container.
					The keys in map are member names, values are widgets or DOM elements
					@function {jgrouse.util.Map} getMembers
				*/				
				getMembers : function()
				{
				},
				
				
				/**
					Call certain callback function for all widgets in layout
					@function forEach
					@param {function(widget, constraint)} callback - function to be called
				*/
				forEach : function(callback)
				{
					this._widgetsMap.forEach(callback);
				},
				
				layout : function()
				{
					
				}
			}
		});
	}
});